<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="info">
	<p><a href="https://github.com/yomotsu/camera-controls">GitHub repo</a></p>
	<label><input type="checkbox" onchange="cameraControls.verticalDragToForward = this.checked">vertical drag to move forward</label>
	<br>
	<label><input type="checkbox" onchange="cameraControls.dollyToCursor = this.checked">dolly to cursor</label>
	<br>
	<label><input type="checkbox" onchange="cameraControls.dollyDragInverted = this.checked">dolly drag inverted</label>
	<br>
	<br>
	<button onclick="cameraControls.rotate( 45 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta 45deg</button>
	<button onclick="cameraControls.rotate( - 90 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta -90deg</button>
	<button onclick="cameraControls.rotate( 360 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta 360deg</button>
	<button onclick="cameraControls.rotate( 0, 20 * THREE.MathUtils.DEG2RAD, true )">rotate phi 20deg</button>
	<br>
	<button onclick="cameraControls.truck(   1,   0, true)">truck( 1, 0 )</button>
	<button onclick="cameraControls.truck(   0,   1, true)">truck( 0, 1 )</button>
	<button onclick="cameraControls.truck( - 1, - 1, true )">truck( -1, -1 )</button>
	<br>
	<button onclick="cameraControls.dolly(   1, true )">dolly  1</button>
	<button onclick="cameraControls.dolly( - 1, true )">dolly -1</button>
	<br>
	<button onclick="cameraControls.zoom( camera.zoom / 2, true )">zoom `camera.zoom / 2`</button>
	<button onclick="cameraControls.zoom( -camera.zoom / 2, true )">zoom `- camera.zoom / 2`</button>
	<br>
	<button onclick="cameraControls.moveTo( 3, 5, 2, true )">move to( 3, 5, 2 )</button>
	<button onclick="cameraControls.fitToBox( mesh, true )">fit to the bounding box of the mesh</button>
	<br>
	<button onclick="cameraControls.normalizeRotations().setPosition( - 5, 2, 1, true )">move to ( -5, 2, 1 )</button>
	<button onclick="cameraControls.normalizeRotations().setTarget( 3, 0, -3, true )">look at ( 3, 0, -3 )</button>
	<button onclick="cameraControls.normalizeRotations().setLookAt( 1, 2, 3, 1, 1, 0, true )">move to ( 1, 2, 3 ), look at ( 1, 1, 0 )</button>
	<br>
	<button onclick="cameraControls.normalizeRotations().lerp( { target: [1, 1, 0], position: [ - 2, 0, 0 ] }, { target: [ -1, 0, 0 ], position: [ 0, 2, 5]}, Math.random(), true )">move to somewhere between position/target: ( -2, 0, 0 ) -> ( 1, 1, 0 ) and ( 0, 2, 5 ) -> ( -1, 0, 0 )</button>
	<button onclick="cameraControls.lerp( { target: [ 1, 1, 0 ], spherical: [ 2, 0, -Math.PI/2 ]}, { target: [ -1, 0, 0 ], spherical: [ 7, Math.PI, Math.PI/2 ]}, Math.random(), true )">move to somewhere inside a hemisphere of a radius ∈ [2..7] centered at a point located between ( 1, 1, 0 ) and ( -1, 0, 0 )</button>
	<br>
	<button onclick="cameraControls.normalizeRotations().reset( true )">reset</button>
	<button onclick="cameraControls.saveState()">saveState</button>
	<br>
	<button onclick="cameraControls.enabled = false;">disable mouse/touch controls</button>
	<button onclick="cameraControls.enabled = true;">enable mouse/touch controls</button>
</div>

<script async src="https://unpkg.com/es-module-shims@2.6.1/dist/es-module-shims.js"></script>
<script type="importmap">
{
	"imports": {
		"three": "https://unpkg.com/three@0.178.0/build/three.module.js",
		"three/addons/": "https://unpkg.com/three@0.178.0/examples/jsm/"
	}
}
</script>
<script type="module">
import * as THREE from 'three';
import CameraControls from './dist/camera-controls.module.js';
CameraControls.install( { THREE: THREE } );

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

const cameraControls = new CameraControls( camera, renderer.domElement );

const mesh = new THREE.Mesh(
	new THREE.BoxGeometry( 1, 1, 1 ),
	new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );

renderer.render( scene, camera );

( function anim () {

	const delta = clock.getDelta();
	const elapsed = clock.getElapsedTime();
	const updated = cameraControls.update( delta );

	// if ( elapsed > 30 ) { return; }

	requestAnimationFrame( anim );

	if ( updated ) {

		renderer.render( scene, camera );
		console.log( 'rendered' );

	}

} )();

// make variable available to browser console
globalThis.THREE = THREE;
globalThis.camera = camera;
globalThis.cameraControls = cameraControls;
globalThis.mesh = mesh;
</script>

</body>
</html>
